package com.thinkgem.jeesite.modules.gen.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.frame.pm.utils.PmParamUtils;
import com.thinkgem.jeesite.modules.gen.GenConstants;
import com.thinkgem.jeesite.modules.gen.entity.*;
import org.apache.commons.collections.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author:Kongxiangsheng
 * @DesCription:
 * @Date:Created in 9:20 2018/11/14
 * @Modified By:
 */
public class ModelUtils {

    public static Map<String, String> getGlobalModel(GenScheme genScheme) {
        Map<String, String> globalMap = Maps.newHashMap();
        GenProject genProject = genScheme.getGenProject();
        GenProjectModule genProjectModule = genScheme.getGenProjectModule();

        String projectCode = genProject.getCode();
        String projectName = genProject.getName();
        String projectBasePackage = StringUtils.lowerCase(genProject.getBasePackage());
        String projectBaseColumns = genProject.getBaseColumns();
        String moduleCode = null == genProjectModule ? "" : genProjectModule.getModuleCode();
        String moduleName = null == genProjectModule ? "" : genProjectModule.getModuleName();
        String basePackage = StringUtils.lowerCase(genScheme.getBasePackage());
        String functionName = genScheme.getFunctionName();
        String author = genScheme.getAuthor();
        List<String> importExport = genScheme.getImportExport();

        // 项目编号
        globalMap.put("projectCode", projectCode);
        globalMap.put("projectCodeUpper", StringUtils.upperCase(projectCode));
        globalMap.put("projectCodeLower", StringUtils.lowerCase(projectCode));

        // 项目编号-驼峰
        String projectCodeCapital = StringUtils.toCapitalizeCamelCase(projectCode);
        globalMap.put("projectCodeCapital", projectCodeCapital);
        globalMap.put("projectCodeUnCapital", StringUtils.uncapitalize(projectCodeCapital));

        // 项目简称
        globalMap.put("projectShortCode", genProject.getShortCode());
        globalMap.put("projectShortCodeUpper", StringUtils.upperCase(genProject.getShortCode()));
        globalMap.put("projectShortCodeLower", StringUtils.lowerCase(genProject.getShortCode()));

        // 项目简称-驼峰
        String projectShortCodeCapital = StringUtils.toCapitalizeCamelCase(genProject.getShortCode());
        globalMap.put("projectShortCodeCapital", projectShortCodeCapital);
        globalMap.put("projectShortCodeUnCapital", StringUtils.uncapitalize(projectShortCodeCapital));

        // 项目名称
        globalMap.put("projectName", projectName);

        // 项目基础路径
        globalMap.put("projectBasePackage", projectBasePackage);
        globalMap.put("projectBasePath", StringUtils.replace(projectBasePackage, ".", "/"));

        // 项目基础字段
        globalMap.put("projectBaseColumns", StringUtils.lowerCase(projectBaseColumns));

        // 模块编号
        globalMap.put("moduleCode", moduleCode);
        globalMap.put("moduleCodeCapital", StringUtils.capitalize(moduleCode));
        globalMap.put("moduleCodeUnCapital", StringUtils.uncapitalize(moduleCode));
        globalMap.put("moduleCodeUpper", StringUtils.upperCase(moduleCode));
        globalMap.put("moduleCodeLower", StringUtils.lowerCase(moduleCode));

        // 模块名称
        globalMap.put("moduleName", moduleName);

        // 子模块编号

        // 子模块名称

        // 基础包路径-分隔符为"."
        globalMap.put("finalBasePackage", basePackage);
        globalMap.put("finalBasePath", StringUtils.replace(basePackage, ".", "/"));

        // 基础包路径-分隔符为"/"

        // 基础包路径-最后一个

        // 功能名
        globalMap.put("functionName", functionName);

        // 作者
        globalMap.put("author", author);

        // 时间
        globalMap.put("dateStr", DateUtils.getDate());

        // 导入导出
        String importFlag = "N", exportFlag = "N";
        if (null != importExport && importExport.contains("import")) {
            importFlag = "Y";
        }
        if (null != importExport && importExport.contains("export")) {
            exportFlag = "Y";
        }
        globalMap.put("import", importFlag);
        globalMap.put("export", exportFlag);

        return globalMap;
    }

    /**
     * entity的import
     *
     * @param genTable
     * @param baseColumns
     * @return
     */
    public static List<String> getEntityImportList(GenTable genTable, List<String> baseColumns) {
        List<GenTableColumn> columnList = genTable.getColumnList();
        List<String> list = Lists.newArrayList();

        // 特殊字段需要引入的内容
        for (GenTableColumn column : columnList) {
            if (baseColumns.contains(column.getName())) {
                continue;
            }

            // 字符串长度
            if ("String".equals(column.getJavaType()) && !"0".equals(column.getDbLength())) {
                addToListIfNotContains(list, "org.hibernate.validator.constraints.Length");
            }

            // 导入Date包-默认引入FastJson
            if ("Date".equals(column.getJavaType())) {
                addToListIfNotContains(list, "java.util.Date");
                addToListIfNotContains(list, "com.alibaba.fastjson.annotation.JSONField");
            }

            // 导入BigDecimal包-默认引入BigDecimal
            if ("BigDecimal".equals(column.getJavaType())) {
                addToListIfNotContains(list, "java.math.BigDecimal");
            }

            if (!"1".equals(column.getIsNull()) && "String".equals(column.getJavaType())) {
                addToListIfNotContains(list, "javax.validation.constraints.NotBlank");
            }

            // 导入TableField和SqlCondition
            if ("like".equals(column.getQueryType())) {
                addToListIfNotContains(list, "com.baomidou.mybatisplus.annotation.SqlCondition");
                addToListIfNotContains(list, "com.baomidou.mybatisplus.annotation.TableField");
            }
        }

        // 如果有子表，则需引入以下内容
        if (CollectionUtils.isNotEmpty(genTable.getChildTableList())) {
            addToListIfNotContains(list, "java.util.List");
            addToListIfNotContains(list, "com.google.common.collect.Lists");
            addToListIfNotContains(list, "com.baomidou.mybatisplus.annotation.TableField");
        }

        // 排序
        Collections.sort(list);
        return list;
    }

    /**
     * service需要引入的内容
     *
     * @return
     */
    public static List<String> getServiceImportList(GenTable genTable) {
        List<String> list = Lists.newArrayList();

        // 必须引入的内容
        addToListIfNotContains(list, "com.baomidou.mybatisplus.extension.service.IService");
        addToListIfNotContains(list, "com.baomidou.mybatisplus.extension.service.impl.ServiceImpl");
        addToListIfNotContains(list, "org.springframework.stereotype.Service");
        addToListIfNotContains(list, "org.springframework.transaction.annotation.Transactional");

        // 如果有子表，则需引入以下内容
        if (CollectionUtils.isNotEmpty(genTable.getChildTableList())) {
            addToListIfNotContains(list, "com.thinkgem.jeesite.common.persistence.BaseConstants");
            addToListIfNotContains(list, "org.springframework.beans.factory.annotation.Autowired");
        }

        // 如果有父表，则需要引入一下内容
        if (StringUtils.isNotBlank(genTable.getParentTable())) {
            addToListIfNotContains(list, "com.baomidou.mybatisplus.core.conditions.query.QueryWrapper");
            addToListIfNotContains(list, "com.baomidou.mybatisplus.mapper.QueryWrapper");
            addToListIfNotContains(list, "java.util.List");
        }

        // 排序
        Collections.sort(list);
        return list;
    }

    /**
     * 获取列注解列表
     *
     * @return
     */
    public static List<String> getAnnotationList(GenTableColumn column) {
        List<String> list = Lists.newArrayList();
        if ("Date".equals(column.getJavaType())) {
            list.add("JSONField(format = \"yyyy-MM-dd HH:mm:ss\")");
        }
        if (!"1".equals(column.getIsNull()) && "String".equals(column.getJavaType())) {
            list.add("NotBlank(message=\"" + column.getComments() + "不能为空\")");
        }
        if ("String".equals(column.getJavaType())) {
            list.add("Length(max=" + column.getDbLength() + ", message=\"" + column.getComments() + "长度必须小于 " + column.getDbLength() + " \")");
        }
        return list;
    }

    /**
     * 往List中添加数据并自动排重
     *
     * @param list
     * @param value
     */
    private static void addToListIfNotContains(List<String> list, String value) {
        if (list.contains(value)) {
            return;
        }
        list.add(value);
    }
}
